package com.atguigu.webadmin.controller.login;

import com.atguigu.common.constant.LoginUserContext;
import com.atguigu.common.result.Result;

import com.atguigu.webadmin.service.LoginService;

import com.atguigu.webadmin.vo.login.CaptchaVo;
import com.atguigu.webadmin.vo.login.LoginVo;
import com.atguigu.webadmin.vo.system.user.SystemUserInfoVo;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Tag(name = "后台管理系统登录管理")
@RestController
@RequestMapping("/admin")
@CrossOrigin
public class LoginController {


    @Autowired
    private LoginService loginService;
    @Operation(summary = "获取图形验证码")
    @GetMapping("login/captcha")
    public Result<CaptchaVo> getCaptcha() {
        CaptchaVo captchaVo = loginService.getCaptcha();
        return Result.ok(captchaVo);
    }

    @Operation(summary = "登录")
    @Parameters({
            @Parameter(name = "loginVo",description = "登录vo对象",in = ParameterIn.DEFAULT)
    })
    @PostMapping("login")
    public Result<String> login(@RequestBody LoginVo loginVo) {
        String token = loginService.login(loginVo);

        return Result.ok(token);
    }

    @Operation(summary = "获取登陆用户个人信息")
    @GetMapping("info")
    public Result<SystemUserInfoVo> info() {
        SystemUserInfoVo user = loginService.getSystemUserInfoById(LoginUserContext.getLoginUser().getUserId());
        return Result.ok(user);
    }
}